import os
import shutil
from win32com.client import Dispatch


def process_flag_directories(root_dir, target_dir):
    # 创建目标目录（如果不存在）
    os.makedirs(target_dir, exist_ok=True)

    for root, dirs, files in os.walk(root_dir):
        if 'flag.txt' in files:
            # 获取当前目录路径
            src_dir = os.path.abspath(root)

            """ 1. 创建快捷方式 """
            shortcut_name = f"{os.path.basename(src_dir)}.lnk"
            shortcut_path = os.path.join(os.path.dirname(src_dir), shortcut_name)

            # 创建快捷方式对象
            shell = Dispatch('WScript.Shell')
            shortcut = shell.CreateShortCut(shortcut_path)
            shortcut.TargetPath = src_dir
            shortcut.WorkingDirectory = src_dir
            shortcut.IconLocation = "%SystemRoot%\\system32\\shell32.dll,3"
            shortcut.save()

            """ 2. 移动目录到目标位置 """
            dest_path = os.path.join(target_dir, os.path.basename(src_dir))
            try:
                shutil.move(src_dir, dest_path)
                print(f"已移动目录: {src_dir} -> {dest_path}")
            except shutil.Error as e:
                print(f"移动失败: {e}")


if __name__ == "__main__":
    # 配置参数
    search_root = "//diskstation3/10T_01/game_backup_test"  # 要遍历的根目录
    output_dir = "//diskstation3/10T_01/game_backup_target"  # 移动目标目录

    process_flag_directories(search_root, output_dir)